package com.wkw.jkyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wkw.jkyf.mapper.DruginfoMapper;
import com.wkw.jkyf.mapper.ReturngoodsMapper;
import com.wkw.jkyf.mapper.SaleinfoMapper;
import com.wkw.jkyf.pojo.Druginfo;
import com.wkw.jkyf.pojo.Problem;
import com.wkw.jkyf.pojo.Returngoods;
import com.wkw.jkyf.pojo.Saleinfo;
import com.wkw.jkyf.service.IReturngoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Time;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

//收到退货
@Service
public class ReturngoodsServiceImpl  extends ServiceImpl<ReturngoodsMapper, Returngoods> implements IReturngoodsService {

    @Resource
    private ReturngoodsMapper returngoodsMapper;
    @Resource
    private DruginfoMapper druginfoMapper;
    @Resource
    private SaleinfoMapper saleinfoMapper;


    @Override
    public IPage<Returngoods> selectReturngoodsPage(int pageNum, int pageSize, String param) {
        QueryWrapper<Returngoods> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(param)){
            queryWrapper.like("dname",param);
        }
        queryWrapper.orderByDesc("id");
        Page<Returngoods> page = new Page<>(pageNum,pageSize);
        return returngoodsMapper.selectPage(page,queryWrapper);
    }

    @Override
    @Transactional
    //收到退货
    public int addReturngoods(Returngoods returngoods) {
        int operateFlag = 0;
        //顾客退回药品数量
        int returnCount = returngoods.getCount();
        QueryWrapper<Saleinfo> saleInfoWrapper = new QueryWrapper<>();
        saleInfoWrapper
                .eq("dname",returngoods.getDname())
                .eq("dnumber",returngoods.getDnumber());
        List<Saleinfo> saleInfo = saleinfoMapper.selectList(saleInfoWrapper);
        int sum = 0;
        //拿到历史这批药品的销售总数量
        if(!CollectionUtils.isEmpty(saleInfo)){
            sum  = saleInfo.stream().mapToInt(Saleinfo::getCount).sum();
        }
        if(returnCount>sum){
            return -4;
        }
        QueryWrapper<Druginfo> drugWrapper = new QueryWrapper<>();
        drugWrapper
                .eq("name",returngoods.getDname())
                .eq("number",returngoods.getDnumber());
        Druginfo druginfo = druginfoMapper.selectOne(drugWrapper);
        //药品库存数量
        int stock = druginfo.getStock();

        druginfo.setStock(stock+returnCount);
        operateFlag += druginfoMapper.updateById(druginfo);


        float price = druginfo.getPrice();
        float resultTotal = price * returnCount;
        //设置总金额
        returngoods.setTotal(resultTotal);
        //设置操作时间
        returngoods.setOperatetime(new Date());
        operateFlag +=  returngoodsMapper.insert(returngoods);
        return operateFlag;
    }

    @Override
    public int editReturngoods(Returngoods returngoods) {
        return returngoodsMapper.updateById(returngoods);
    }
    @Override
    public Returngoods queryReturngoodsById(Integer id) {
        return returngoodsMapper.selectById(id);
    }

    @Override
    public int deleteReturngoodsById(Integer id) {
        Returngoods returngoods = returngoodsMapper.selectById(id);
        //查询出来的数量
        Integer count = returngoods.getCount();
        QueryWrapper<Druginfo> drugWrapper = new QueryWrapper<>();
        drugWrapper
                .eq("name",returngoods.getDname())
                .eq("number",returngoods.getDnumber());
        Druginfo druginfo = druginfoMapper.selectOne(drugWrapper);
        druginfo.setStock(druginfo.getStock()-count);
        druginfoMapper.updateById(druginfo);
        return returngoodsMapper.deleteById(id);
    }
}
